#include "BootSetting.h"
#include<QJsonDocument>
#include<QJsonObject>
#include<QJsonArray>
#include<QFile>

#define BOOT_SETTING_JSON   "config/bootSetting.json"

namespace RuntimeData {
BootSetting::BootSetting()
{
}

BootSetting *BootSetting::getInstance()
{
    static BootSetting singleton{};
    return &singleton;
}

void BootSetting::loadBootSettingInfo()
{
    QFile file(BOOT_SETTING_JSON);
    QJsonObject obj;

    if (file.exists()) {
        iLog(QString("%1 file exists").arg(BOOT_SETTING_JSON));
        readBootSettingInfoByRom();
        return;
    }
    if (!file.open(QIODevice::ReadWrite)) {
        iLog(QString("%1 open error, use default bootSetting").arg(BOOT_SETTING_JSON));
        setDefalutBootSettingInfo();
        system("mkdir -p /home/root/config && touch /home/root/config/bootSetting.json");

        saveBootSettingInfoToRom();
        return;
    }
    setDefalutBootSettingInfo();

    obj["touchSound"]        = g_bootSettingInfo.touchSound;
    obj["isFirstTimeLogin"]  = g_bootSettingInfo.isFirstTimeLogin;
    obj["loginMode"]         = g_bootSettingInfo.loginMode;
    obj["languageIndex"]     = g_bootSettingInfo.languageIndex;
    obj["isLogin"]           = g_bootSettingInfo.isLogin;
    obj["currentUserId"]     = g_bootSettingInfo.currentUserId;
    obj["currentPatientId"]  = g_bootSettingInfo.currentPatientId;
    obj["currentMirrorType"] = g_bootSettingInfo.currentMirrorType;
    obj["superPassword"]     = g_bootSettingInfo.superPassword;

    QJsonDocument jdoc(obj);
    file.write(jdoc.toJson());
    file.flush();
    file.close();
}

void BootSetting::saveBootSettingInfoToRom()
{
    QFile file(BOOT_SETTING_JSON);
    QJsonObject obj;

    file.open(QIODevice::WriteOnly);
    obj["touchSound"] = g_bootSettingInfo.touchSound;
    obj["isFirstTimeLogin"] = g_bootSettingInfo.isFirstTimeLogin;
    obj["loginMode"] = g_bootSettingInfo.loginMode;
    obj["languageIndex"] = g_bootSettingInfo.languageIndex;
    obj["isLogin"] = g_bootSettingInfo.isLogin;
    obj["currentUserId"] = g_bootSettingInfo.currentUserId;
    obj["currentPatientId"] = g_bootSettingInfo.currentPatientId;
    obj["currentMirrorType"] = g_bootSettingInfo.currentMirrorType;
    obj["superPassword"] = g_bootSettingInfo.superPassword;

    QJsonDocument jdoc(obj);
    file.write(jdoc.toJson());
    file.flush();
    file.close();
}

void BootSetting::readBootSettingInfoByRom()
{
    QFile file(BOOT_SETTING_JSON);
    QByteArray data;
    QJsonDocument doc;
    QJsonObject obj;

    if (!file.open(QIODevice::ReadOnly)) {
        iLog(QString("%1 open error, use default bootSetting").arg(BOOT_SETTING_JSON));
        setDefalutBootSettingInfo();
        return;
    }
    data = file.readAll();
    file.close();

    doc = QJsonDocument::fromJson(data);
    if(doc.isObject()) {
        /* 把json文档转换为json对象 */
        obj = doc.object();
        g_bootSettingInfo.touchSound = obj.value("touchSound").toInt();
        g_bootSettingInfo.isFirstTimeLogin = obj.value("isFirstTimeLogin").toInt();
        g_bootSettingInfo.loginMode = obj.value("loginMode").toInt();
        g_bootSettingInfo.languageIndex = obj.value("languageIndex").toInt();
        g_bootSettingInfo.isLogin = obj.value("isLogin").toInt();
        g_bootSettingInfo.currentUserId = obj.value("currentUserId").toInt();
        g_bootSettingInfo.currentPatientId = obj.value("currentPatientId").toInt();
        g_bootSettingInfo.currentMirrorType = obj.value("currentMirrorType").toInt();
        g_bootSettingInfo.superPassword = obj.value("superPassword").toString();
    }
}

void BootSetting::setDefalutBootSettingInfo()
{
    g_bootSettingInfo.touchSound = -1;/* 0：有声音  -1：静音 */
    g_bootSettingInfo.isFirstTimeLogin = 0;/* 0：首次使用  1：非首次使用 */
    g_bootSettingInfo.loginMode = 0;/* 0：需要登录 1：直接进入主界面 */
    g_bootSettingInfo.languageIndex = 0;/* 0：简体中文  1：繁体中文  2：英文 */
    g_bootSettingInfo.isLogin = 0;/* 0：未登录 1：已登录 2：管理员登录 */
    g_bootSettingInfo.currentUserId = -1;/* /* -1：未登录 其他：医生id */
    g_bootSettingInfo.currentPatientId = -1;/* 当前病人id */
    g_bootSettingInfo.currentMirrorType = 0;/* 0：默认 1：自定义1 2：自定义2 3：宫腔镜 4：膀胱镜 5：肾盂镜 */
    g_bootSettingInfo.superPassword = "888888";/* 超级密码 */
}
}
